package com.example.config.securityConfig.authAction;

import com.alibaba.fastjson.JSON;
import com.example.common.utils.ApiResult;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户登出类
 * @Author Sans
 * @CreateTime 2019/10/3 9:42
 */
@Component
public class UserLogoutSuccessHandler implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        httpServletResponse.setCharacterEncoding("utf-8");    //设置 HttpServletResponse使用utf-8编码
        httpServletResponse.setHeader("Content-Type", "text/html;charset=utf-8");  //设置响应头的编码
        ApiResult apiResult = new ApiResult(Boolean.TRUE);
        apiResult.setData(0);
        apiResult.setMessage("退出成功");
        httpServletResponse.getWriter().write(JSON.toJSON(apiResult).toString());
    }

}
